<?php

// phpMyRealty 3
//
// File Name: captcha.php
// File Location : ./includes/functions/
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// ----------------------------------------------------------------------------
// generateCaptcha()
//
// generates captcha random string
//

function generateCaptcha() {

 global $_SESSION;

 if (!isset($_SESSION['random'])) {
  session_register ( "random" );
  $_SESSION['random'] = generateUniqueID();
 }

}

// ----------------------------------------------------------------------------
// generateUniqueID()
//
// generates captcha random string
//

function generateUniqueID($length = 7) {

 $charList = '';
 $uniqueID = '';

 for ( $preCount = 65 , $postCount = 97; $preCount < 91 && $postCount < 123; $preCount++, $postCount++ ) {
  $charList[] = pack('C' , $preCount);
  $charList[] = pack('C' , $postCount);
 }

 for ( $counter = 0; $counter < $length; $counter++ ) {
  $uniqueID .= $charList[rand(0 , (count($charList) - 1))];
 }

return(strtoupper($uniqueID));

}

// ----------------------------------------------------------------------------
// generateCaptchaImage()
//
// generates captcha image using random string
//
// $string - random string
//

function generateCaptchaImage($string) {

 $image = ImageCreateTrueColor(130, 30);
 $color = ImageColorAllocate($image, 255, 255, 255);
 ImageFill($image, 0, 0, $color);

 $text_color = ImageColorAllocate($image, 0, 0, 0);

 Header("Content-Type: image/jpeg");

 if (function_exists('imagettftext')) {
  $xShift = 10;
  $string = str_split($string);

  for ($a = 0; $a < count($string); $a++) {
   $stringIn = $string[$a];
   $angle = rand ( -20, 40 );
   $size = rand ( 12, 16 );
   Imagettftext ($image, $size, $angle, $xShift, 22, $text_color, PATH . '/fonts/freefont.ttf', $stringIn) or die ("error");
   $xShift = $xShift + 17;
  }

 }
 else
  ImageString ($image, 5, 7, 2, $string, $text_color);

 if (function_exists('ImageFilter'))
  ImageFilter ($image, 10, 15);

 ImageJPEG($image, '', 100);

 ImageDestroy($image);

}

?>